class Solution {
public:
    int monotoneIncreasingDigits(int n) {
        string num=to_string(n);
        int pos=0;
        while(pos+1<num.size() && num[pos]<=num[pos+1])
            pos++;
        
        if(pos==num.size()-1)
            return n;
        while(pos-1>=0 && num[pos]==num[pos-1])
            pos--;
        num[pos]=num[pos]-'0'-1+'0';
        ++pos;
        while(pos<num.size())
            num[pos++]='9';
        return stoi(num);
    }
};
